﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IChessEngine;

namespace ChessControls
{
    /// <summary>
    /// Interaction logic for ChsBoardControl.xaml
    /// </summary>
    public partial class ChsBoardControl : UserControl
    {
        private IChsPosition position = null;
        private IChsValidator validator = ChessEngine.ChsFactory.CreateValidator();
        public ChsBoardControl()
        {
            InitializeComponent();
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                {
                    ChsPieceControl piece = (ChsPieceControl)FindName(String.Format("ImgSquare{0}{1}", x, y));
                    piece.Square = ChessEngine.ChsFactory.CreateSquare(y,x);
                }
        }
        public void SetPosition(IChsPosition _position)
        {
            position = _position;
            for(int x=0;x<8;x++)
                for (int y = 0; y < 8; y++)
                {
                    ChsPieceControl square = (ChsPieceControl)FindName(String.Format("ImgSquare{0}{1}", x, y));
                    square.Piece = position.GetPieceAt(y,x);
                }
        }
        private void Grid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("ChsSquare"))
            {
                IChsSquare target = PointToCell(e.GetPosition(grid));
                IChsSquare source = (IChsSquare)e.Data.GetData("ChsSquare");
                IChsMovement movement = null;
                if (position.IsPromotingMovement(source, target))
                {
                    PromotingWindow promotingWindow = new PromotingWindow();
                    promotingWindow.ShowDialog();
                    movement = ChessEngine.ChsFactory.CreateMovement(source, target, promotingWindow.type);
                }
                else
                    movement = ChessEngine.ChsFactory.CreateMovement(source, target);
                if(Movement!=null)
                    Movement(this, new ChsMovementEventArgs(movement));
                
            }
        }
        private IChsSquare PointToCell(Point point)
        {
            int x = 0;
            while ((point.X > grid.ColumnDefinitions[x].ActualWidth)&&(x<8))
            {
                point.X -= grid.ColumnDefinitions[x].ActualWidth;
                x++;
            }
            int y = 0;
            while ((point.Y > grid.RowDefinitions[y].ActualHeight)&&(y<8))
            {
                point.Y -= grid.RowDefinitions[y].ActualHeight;
                y++;
            }
            return ChessEngine.ChsFactory.CreateSquare(x, 7 - y);
        }
        public event ChsMovementEventHandler Movement;
    }
}
